//
//  Int+To.swift
//  TTCOM-iOS
//
//  Created by Toj on 2/10/23.
//

import Foundation

// MARK: - To Font
public extension Int {
    
    // MARK: - System font
    var to_systemFont: UIFont {
        return CGFloat(self).to_systemFont
    }
    var to_font: UIFont {
        return CGFloat(self).to_systemFont
    }
    
    var to_boldFont: UIFont {
        return CGFloat(self).to_boldFont
    }
    
    // MARK: - Roboto font
    var to_rbtLightFont: UIFont {
        return to_lightFont(name: .roboto) ?? to_systemFont
    }
    
    var to_rbtRegularFont: UIFont {
        return to_regularFont(name: .roboto) ?? to_systemFont
    }
    
    var to_rbtMediumFont: UIFont {
        return to_mediumFont(name: .roboto) ?? to_systemFont
    }
    
    var to_rbtBoldFont: UIFont {
        return to_boldFont(name: .roboto) ?? to_boldFont
    }
    
    // MARK: - Custom font
    /// 细体: 300
    func to_lightFont(name: UIFont.TTName) -> UIFont? {
        return CGFloat(self).to_lightFont(name: name)
    }
    
    /// 标准: 400
    func to_regularFont(name: UIFont.TTName) -> UIFont? {
        return CGFloat(self).to_regularFont(name: name)
    }
    
    /// 适中: 500
    func to_mediumFont(name: UIFont.TTName) -> UIFont? {
        return CGFloat(self).to_mediumFont(name: name)
    }
    
    /// 粗体: 700
    func to_boldFont(name: UIFont.TTName) -> UIFont? {
        return CGFloat(self).to_boldFont(name: name)
    }
}

// MARK: - To
public extension Int {
    
    var to_color: UIColor {
        return UIColor(hex: self)
    }
    
    var to_pi: CGFloat {
        return CGFloat(self) / 180 * CGFloat.pi
    }
}

// MARK: - 时分秒 转 格式化
public extension Int {
    
    /**
     秒转时间字符串
     - NOTE: 格式必须与时间一一对应否则会出错
     
     - parameter format: 返回格式
     - returns: 格式
     
     ```
     98945 = ((24 + 3) * 60 + 29) * 60 + 5
     98945.to_timeString("%d天%d时%d分%d秒")
     return 1天3时29分5秒
     ```
     */
    func to_timeString(_ format: String) -> String {
        // 秒
        var second = Int64(self)
        if second < 60 {
            return String(format: format, second)
        }
        
        // 分 秒
        var minute = second / 60
        second = second % 60
        if minute < 60 {
            return String(format: format, minute, second)
        }
        
        // 时 分 秒
        var hour = minute / 60
        minute = minute % 60
        if hour < 24 {
            return String(format: format, hour, minute, second)
        }
        
        // 天 时 分 秒
        let day = hour / 24
        hour = hour % 24
        return String(format: format, day, hour, minute, second)
    }
    
    /**
     秒带单位
     
     - returns:ns
     ```
     98945.to_timeString_s
     return 98945s
     ```
     */
    var to_timeString_s: String {
        return to_timeString(second: "s")
    }
    
    func to_timeString(second unit: String = "") -> String {
        let result = "\(self)" + unit
        return result
    }
    
    var to_timeString_ms: String {
        return to_timeString(minute: "m", second: "s")
    }
    
    func to_timeString(
        minute mseparator: String,
        second sseparator: String = ""
    ) -> String {
        let minites = self / 60
        let seconds = self % 60
        
        let dformat = mseparator != ":" ? "%d%@" : "%02d%@"
        
        let result =
        String(format: dformat, minites, mseparator) +
        String(format: dformat, seconds, sseparator)
        
        return result
    }
    
    var to_timeString: String {
        return to_timeString(hour: ":", minute: ":", second: "")
    }
    
    var to_timeString_hms: String {
        return to_timeString(hour: "h", minute: "m", second: "s")
    }
    
    func to_timeString(
        hour   hseparator: String = ":",
        minute mseparator: String = ":",
        second sseparator: String = ""
    ) -> String {
        let hours   = self / 3600
        let minites = self / 60 % 60
        let seconds = self % 60
        
        let dformat = (hseparator != ":" || mseparator != ":") ? "%d%@" : "%02d%@"
        
        var result = ""
        if hours > 0 { result = String(format: "%d%@", hours, hseparator) }
        let format = hours > 0 ? "%02d%@" : dformat
        result = result + String(format: format, minites, mseparator)
        result = result + String(format: dformat, seconds, sseparator)
        
        return result
    }
}

// MARK: - string 高度
public extension Int {
    
    /**
     获取不同行数高度
     
     - Parameter font: 字体大小
     */
    func st_stringLineHeight(font: TTFont) -> CGFloat {
        let line = self
        guard line > 0 else { return 0 }
        
        var lineString = "*"
        for _ in 1..<line {
            lineString += "\n*"
        }
        let height = lineString.cm_stringHeight(font: font, maxWidth: 1)
        return height
    }
}
